#!/usr/bin/perl -w

# USAGE:
# Get the number and write to the file outfile.txt
# wget http://www.thesixtyone.com -O - | ./get_t61_num.pl -o outfile.txt
# 

use strict;
use Getopt::Long;

my $infile;
my $outfile;
my $verbose;

#-----------------------------+
# GET COMMAND LINE OPTIONS    |
#-----------------------------+
my $ok = GetOptions(# REQUIRED OPTIONS                                              
                    "i|infile=s"  => \$infile,
                    "o|outfile=s" => \$outfile,
		    "verbose"     => \$verbose,
    );

#-----------------------------+
# OPEN IO FILES               |
#-----------------------------+
if ($infile) {
    open (INFILE, "<$infile");
}
else {
    open (INFILE, "<&STDIN");
}
    
# Append to an existing output file
if ($outfile) {
    open (OUTFILE, ">>$outfile");
} 
else {
    open (OUTFILE, ">&STDOUT");
}

#-----------------------------+
# GET TIMESTAMP DATA          | 
#-----------------------------+
my ($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime(time);

#-----------------------------+
# PARSE FOR DESIRED INFO      |
#-----------------------------+
while (<INFILE>) {
# New method 03/10/2009
    #<span id="num_listeners">4060</span>
# old_style
#    if ( m/t61\.num_listeners \= (.*)\;/) {
    if ( m/num_listeners\"\>([0-9]*)/) {

	print STDERR $_."\n" if $verbose;
	print STDERR "EXTRACTED:".$1."\n" if $verbose;

	# Print time stampe
	printf OUTFILE "%4d-%02d-%02d %02d:%02d:%02d\t",
	$year+1900,$mon+1,$mday,$hour,$min,$sec;

	# Print the number of listeners
	print OUTFILE $1."\n";

    }
}

#-----------------------------+
# CLOSE FILEHANDLES           |
#-----------------------------+
close (INFILE);
close (OUTFILE);

exit;

# 
# The regext sytax ..
#/t61\.num_listeners \= (.*)\;/
